﻿using System;
using Coolite.Ext.Web;
using Pap.Data;
using Pap.Web.Modules.Loan.ViewModel;
using Pap.Web.Utils;
using Utils.UIPermission;

namespace Pap.Web.Modules.Loan.Views
{
    /// <summary>
    /// 
    /// </summary>
    public partial class AssociationDues : BasePage<AssociationDuesViewModel>, IPermittedPage
    {
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack && !Ext.IsAjaxRequest)
            {
                this.uxAllDues.Pressed = true;
                btnAllDues_Click(this.uxAllDues, null);
            }
        }

        /// <summary>
        /// Handles the Command event of the GrvDueLoans control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Coolite.Ext.Web.AjaxEventArgs"/> instance containing the event data.</param>
        protected void GrvDueLoans_Command(object sender, AjaxEventArgs e)
        {
            if (e.ExtraParams["CommandName"].Equals("PayInstallment"))
            {
                if (e.ExtraParams["LoanExecutionInstallmentID"] != null)
                {
                    var installmentId = e.ExtraParams["LoanExecutionInstallmentID"];
                    this.RedirectFromThisPage("InstallmentsPayment", new string[] { "InstallmentID=" + installmentId });
                }
            }
        }

        /// <summary>
        /// Handles the Clicked event of the BtnBack control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Coolite.Ext.Web.AjaxEventArgs"/> instance containing the event data.</param>
        protected void BtnBack_Clicked(object sender, AjaxEventArgs e)
        {
            this.RedirectToPreviousPage();
        }

        protected void BtnLateDues_Click(object sender, AjaxEventArgs e)
        {
            Button button = sender as Button;
            if (button.Pressed)
            {
                this.uxGrid.Disabled = false;
                storeLoans.DataSource = this.ViewModel.GetLateDues();
                storeLoans.DataBind();
            }
            else
            {
                this.uxGrid.Disabled = true;
            }
        }
        protected void btnAllDues_Click(object sender, AjaxEventArgs e)
        {
            Button button = sender as Button;
            if (button.Pressed)
            {
                this.uxGrid.Disabled = false;
                storeLoans.DataSource = this.ViewModel.GetAllDues();
                storeLoans.DataBind();
            }
            else
            {
                this.uxGrid.Disabled = true;
            }
        }
        protected void BtnTodayDues_Click(object sender, AjaxEventArgs e)
        {
            Button button = sender as Button;
            if (button.Pressed)
            {
                this.uxGrid.Disabled = false;
                storeLoans.DataSource = this.ViewModel.GetTodatDues();
                storeLoans.DataBind();
            }
            else
            {
                this.uxGrid.Disabled = true;
            }
        }
        protected void BtnDuesForInterval_Click(object sender, AjaxEventArgs e)
        {
            this.uxGrid.Disabled = false;
            storeLoans.DataSource = this.ViewModel.GetDuesInInterval(DuesFromDate.SelectedDate,
                                                                     DuesToDate.SelectedDate);
            storeLoans.DataBind();
        }

        public string PermissionName
        {
            get { return Permissions.AssociationDues.ToString(); }
        }
    }
}
